package com.xinya.wanandroidclient.ui.me

import androidx.lifecycle.MutableLiveData
import com.xinya.wanandroidclient.base.BaseRepository
import com.xinya.wanandroidclient.bean.UserInfoBean
import com.xinya.wanandroidclient.eventbus.bean.LogoutMessage
import com.xinya.wanandroidclient.http.ApiException
import com.xinya.wanandroidclient.http.RetrofitManager
import com.xinya.wanandroidclient.http.WanAndroidApiService
import com.xinya.wanandroidclient.utils.UserSPUtils
import kotlinx.coroutines.CoroutineScope
import org.greenrobot.eventbus.EventBus

/**
 *
 * @ClassName:      MeRepo$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/3/10$ 10:36$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class MeRepo(coroutineScope: CoroutineScope) : BaseRepository(coroutineScope) {
    fun getUserInfo(
        userInfoLiveData: MutableLiveData<UserInfoBean>,
        errorLiveData: MutableLiveData<ApiException>
    ) {
        launch(
            block = {
                RetrofitManager.getApiService(WanAndroidApiService::class.java).getUserInfo()
                    .result()
            },
            success = {
                UserSPUtils.INST.saveUserInfo(it)
                userInfoLiveData.postValue(it)
            },
            error = {
                errorLiveData.postValue(it)
            }
        )
    }

    fun logout() {
        launch(
            block = {
                RetrofitManager.getApiService(WanAndroidApiService::class.java).logout()
                    .result(Any::class.java)
            },
            success = {
                UserSPUtils.INST.clearUserInfo()
                EventBus.getDefault().post(LogoutMessage())
            },
            error = {
            }
        )
    }

}